;sharper_collapse.du
;avance-version (18/10/2022)

;written for TopSpin 4 and NEO console, for avIII+ the dwl_clk_on/off commands are different; see the acquisition part
;
;pulse sequence to collapse all or some (selected by band selective perfect echo) signals into a singlet
;solvent presaturation using pulsed presat or changing frequencies
;optional 13C decoupling
;
;G. Peat, P.J. Boaler, C.L. Dickson, G.C. Lloyd-Jones & D. Uhrin, Nat. Commun., 2023. 
;solvent suppression based on:
;Kew, W., Bell, N.G.A. Goodall, I., Uhrin, D., Magn. Reson. Chem. 55, 785-796, (2017)
;
;For mutli-resonance suppression use Multi-reson-suppress.xlsx and PresatOptimise.jl to optimise p23 and o1
;
;optional band selective excitation based on perfect echo
;J. A. Aguilar, M. Nilsson, G. Bodenhausen and G. A. Morris, Chem. Commun., 2012, 48, 811
;
;for SHARRPER papers see 
;Jones, A.B., Lloyd-Jones, G.C., Uhrin D, Anal. Chem. 89 10013-10021 (2017)
;Dickson, C.L., Peat, G. Rossetto, M., Halse, M.E. and Uhrin, D. Chem. Commun., 58, 5534-5537 (2022)
;Davy, M., Dickson, C.L., Wei, R., Uhrin, D., Butts, C.P. Analyst, 147, 1702-1708 (2022)
;Silva-Terra, A. I., Rossetto, M., Dickson, C. L., Peat, G., Uhrin D., Halse, M. E. ACS Meas Sci Au. 3, 73-81 (2022).


;$CLASS=HighRes
;$DIM=1D
;$TYPE=
;$SUBTYPE=
;$COMMENT=

#include <Avance.incl>
#include <Grad.incl>
#include <Delay.incl>
#include <De.incl>

#ifdef P90
"p6=p5"
#else
"p6=p5*2"
#endif

"d11=30m"
"d12=20u"

"d62=dw*l2"

# ifdef PURGE
"d13=4u"
"d63=(d62/2)-cnst13*dw"
"de=cnst13*dw-p1*2/PI"
# else
"d13=de"
"d63=d62/2"
"d14=d16+p1*2/PI-de"
"acqt0=0"
baseopt_echo
# endif

"l0=(aq/d62-0.5)/2"
"l31=l0+4"

# ifdef PULSED_PRESAT
"FACTOR1=(d1/(p23))+ 0.5"
"l6=FACTOR1"
# endif

"acqt0=0"
 baseopt_echo

dwellmode explicit

1 ze

# ifdef C13_DEC
  d12 pl12:f2
# endif

2 d11

# ifdef BSPE
  50u BLKGRAD
# endif

# ifdef PRESAT

# ifdef C13_DEC
  d12 cpds2:f2
# endif

# ifdef PULSED_PRESAT
  d12 pl0:f1
3 (p23:sp23 ph29):f1
  2u
  lo to 3 times l6
  d12 pl1:f1
# endif

#ifdef PRESAT_JUMP
  d12  fq=cnst23(bf):f1 ;solvent offset in Hz
  d12 pl9:f1
  d1 cw:f1 ph29
  4u do:f1
  d12 pl1:f1
  d12  fq=cnst24(bf):f1 ; real offset o1 in Hz
#endif

# ifdef C13_DEC
  d12 do:f2
# endif

# else
  d1 pl1:f1
#endif

# ifdef BSPE
  50u UNBLKGRAD
# endif

  (p1 ph1)

#ifdef BSPE

  d13
  p19:gp1
  d16 pl0:f1

  4u
  (p11:sp1 ph2):f1
  4u

  p19:gp1
  d16
  d13 pl1:f1

  (p1 ph3)
  
  d13 
  p19:gp2
  d16 pl0:f1

  4u
  (p11:sp1 ph4):f1
  4u

  p19:gp2
 
# ifdef PURGE
  d16 pl1:f1
  d13 

# ifdef PURGE1
  p1 ph5
  4u
  p19:gp3
  d16
  p1 ph6
# endif

# ifdef PURGE2
  p1 ph5
  4u
  10u gron0
  (p32:sp29 ph1):f1
  20u groff
  d16
  p16:gp3
  d16 pl1:f1
  p1 ph6
# endif

# else
  d14
# endif

#endif 


	ACQ_START(ph30,ph31)
	0.05u START_NEXT_SCAN  ;Topspin3: leave out
        0.1u REC_UNBLK
  	0.05u DWELL_RELEASE  ;Topspin3: replace with DWL_CLK_ON
	d63 pl5:f1
	0.05u DWELL_HOLD  ;Topspin3: replace with DWL_CLK_OFF
	0.1u REC_BLK


4	d10 
       (p6 ph7):f1
	d10

	0.1u REC_UNBLK
	0.05u DWELL_RELEASE  ;Topspin3: replace with DWL_CLK_ON
	d62
	0.05u DWELL_HOLD  ;Topspin3: replace with DWL_CLK_OFF
	0.1u REC_BLK

	d10 
	(p6 ph8):f1
	d10

	0.1u REC_UNBLK
	0.05u DWELL_RELEASE  ;Topspin3: replace with DWL_CLK_ON
	d62
	0.05u DWELL_HOLD  ;Topspin3: replace with DWL_CLK_OFF
	0.1u REC_BLK

	lo to 4 times l31
	d63
        

	rcyc=2
	d11 mc #0 to 2 F0(zd)

# ifdef BSPE
  50u BLKGRAD
# endif
exit

ph1=0 0 0 0 2 2 2 2
ph2=1 1 1 1 1 1 1 1 3 3 3 3 3 3 3 3
ph3=1 1 3 3
ph4=1 1 1 1 1 1 1 1 3 3 3 3 3 3 3 3
ph5=2
ph6=0
ph7=1 3
ph8=1 3
ph29=0
ph30=(360) 0 ;set phcor30 to direct signal into one channel
ph31=0 0 0 0 2 2 2 2 


;for presat set zgoptns -DPRESAT -DPULSED_PRESAT or zgoptns -DPRESAT -DPRESAT_JUMP
;for selective 13C decoupling zgoptns -DC13_DEC
;for band selective perfect echo set zgoptns -DBSPE
;for z-filter set zgoptns -DPURGE -DPURGE1 or zgoptns -DPURGE -DPURGE2
;for 90 deg spin-echo pulses set zgoptns -DP90

;DIGMOD = baseopt
;d1 : relaxation delay [1-5 * T1]
;d11: delay for disk I/O [30 msec]
;d12: delay for power switching                         [20 usec]
;d16: delay for homospoil/gradient recovery             [200 usec]
;d10: ringdown time - microseconds to hudreads of microseconds
;p1 : f1 channel - 90 degree high power pulse
;p5 : f1 channel - 90 degree lower power pulse for spin-echoes
;p6 : f1 channel - 180 degree lower power pulse for spin-echoes
;pl1 : f1 channel - power level for pulse (default)
;pldb1 : f1 channel -  high power       
;pl5 : f1 channel - power level for spin-echo pulses IMPORTANT! (see SI of the Peat et al paper) 
;pldb5 : f1 channel -  lower power       

;ns: 2 * n, total number of scans: NS * TD0
;ds: 4
;p19: 600us homospoil/gradient pulse
;p16:  1ms  homospoil/gradient pulse

;p23 : ~50 ms low power rectangular pulse
;if off-resonance, calculate its exact length, (see SI of the Peat et al paper) 
;if only one signal needs to be suppressed:
;SPNAM23 : Squa100.1000
;SPOFFS23 : o1 - resonance frequency of the suppressed signal [Hz]
;spdb23 : use minimum power start with 69dB

;for zgoptns -DPRESAT_JUMP set 
;cnst23: signal to be suppressed [Hz]
;cnst24: o1 [Hz]

;for zgoptns -DPULSED_PRESAT set 
;p23 : ~50 ms low power rectangular pulse
;if off-resonance, calculate its exact length as desctiben in the Supplementary Information 

;if only one signal needs to be suppressed:
;SPNAM23 : Squa100.1000
;SPOFFS23 : resonance frequency of the suppressed signal minus o1 [Hz]
;spdb23 : use minimum power start with 60dB, asses the result

;if multiple signals need to be suppressed use Multi-reson-suppress.xlsx and PresatOptimise.jl to optimise p23 and o1
;create a phase-ramped shape using the calulated pulse length and the distances from o1
;prepare a rectangular shape and place it into the user library, e.g. Squa100.1000 (100us, 1000 points)
;on a topspin command line type:
;st manipulate /opt/topspin4.1/exp/stan/nmr/lists/wave/user/Squa100.1000 offs e 68936 3 565.73 -580.27 -754.27
;this will OVERWRITE the Squa100.1000 shape in /opt/topspin4.1/exp/stan/nmr/lists/wave/user/
;creating a 68936 us shape that will irradiate at 565.73, -580.27 and -754.27Hz away from optimised o1
;Note: for n frequencies replace 3 with n
;spdb23 : use minimum power, start with 60dB, asses the result. For every addtional frequency adjust by subtracting 6dB.

;if zgoptns -DC13_DEC:
;o2p : 13C chemical shift of the suppressed solvent signal
;pldb12 : power level for 13C decoupling
;at least 12dB weaker than regular decoupler power
;pcpd2 : around 200us for selective decoupling
;cpdprg2 : xy32 or garp4

;l0 : number of blocks during acquisition time adjusted to get d62 as required
;l2 : number of points in full chunk [2-16] depending on dw = 1/2*SWH (increase SWH to decrease dw) 
;d62: dw*l2 acquisition block between decoupling pulses [< 0.5msec]
;d63: = d62/2, length of initial half chunk if PURGE not used
;d63: d62/2-cnst13*dw if PURGE used
;cnst13: typically 1,increse SWH if negative 
;d10: delay around spin-echo pulses [ 5-20 us] 

;for zgoptns -DBSPE
;p11: f1 channel - 180 degree shaped pulse (Reburp.1000)   [0.5-3 msec]
;pl9 : f1 channel - power level for presaturation [~55-69dB]
;sp1:f1 channel - 180 degree shaped pulse (Reburp.1000)   [0.5-3 msec]
;spnam1: Reburp.1000
;spdb1: power level for sp1

;for zgoptns -DPURGE2 set
;sp29: f1 channel - shaped pulse (adiabatic)
;p32: f1 channel - 180 degree shaped pulse (adiabatic)    [20 msec]
;     smoothed chirp (sweep width, 20% smoothing, 10000 points)

; Instructions for setting receiver phase, ph30
;   1) Record spectrum with PH30 = (360) 0 and phcor30=0
;   2) Phase the spectrum using PHC0 only
;   3) If PHCO > 0 set phcor30 = 360-PHC0, if PHC0 < 0 set phcor = -PHC0
;   4) Set PHC0=0 PHC1=0
;   5) If the phase changes, rephase, do not store PHC0, set phcor30 = phcor30 - PHC0

;for z-only gradients:
;gpz0: ca 11%
;gpz1: 7%
;gpz2: 5%
;gpz3: -12%

;use gradient files:
;gpnam1: SMSQ10.100 or SINE.100
;gpnam2: SMSQ10.100 or SINE.100
;gpnam3: SMSQ10.100 or SINE.100
